//**************************************************************************
// Copyright 2008 Peter Project (http://www.codeplex.com/peter)
//
// License : GNU General Public License (GPL)
// For full text of the license please goto:
// http://www.gnu.org/licenses/gpl-2.0.html
//
// This code is provided on an AS IS basis, with no WARRANTIES,
// CONDITIONS or GUARANTEES of any kind.
//
//		File: Webber.cs
//
//**************************************************************************

using System.Windows.Forms;
using mshtml;

namespace InternetBrowser
{
    ///<summary>Specialization of WebBrowser that providers helper methods that ease usage.</summary>
    public class Webber : WebBrowser
    {
        ///<summary>Default Constructor - opens about:blank page</summary>
        public Webber()
        {
            this.Navigate("about:Blank");
            while (this.Document.Body == null) Application.DoEvents();
        }

        /// <summary>
        /// Get the current selection.
        /// </summary>
        public string Selection
        {
            get
            {
                IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
                IHTMLSelectionObject sel = doc.selection;
                IHTMLTxtRange range = (IHTMLTxtRange)sel.createRange();
                if (range.text == null)
                    return "";
                else
                    return range.text.Trim();
            }
        }

        /// <summary>
        /// Sets the HTML of the document giving the ability to use CSS and scripts.
        /// </summary>
        public string HTML
        {
            set
            {
                IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
                doc.write(value);
                doc.close();
            }
        }

        /// <summary>
        /// Select the word clicked.
        /// </summary>
        public void SelectWord()
        {
            IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
            IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
            IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
            rng.expand("word");
            rng.select();
        }

        /// <summary>
        /// Clear any selection.
        /// </summary>
        public void ClearSelection()
        {
            IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
            IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
            sel.empty();
        }

        /// <summary>
        /// Copy Current Selection.
        /// </summary>
        public void Copy()
        {
            this.Document.ExecCommand("Copy", false, null);
            ClearSelection();
        }

        /// <summary>
        /// Paste data from the clipboard.
        /// </summary>
        public void Paste()
        {
            this.Document.ExecCommand("Paste", false, null);
        }

        /// <summary>
        /// Cut Current Selection.
        /// </summary>
        public void Cut()
        {
            this.Document.ExecCommand("Cut", false, null);
        }

        /// <summary>
        /// Select Everything in the Document.
        /// </summary>
        public void SelectAll()
        {
            this.Document.ExecCommand("SelectAll", false, null);
        }

        /// <summary>
        /// Deletes the Current Selection...
        /// </summary>
        public void Delete()
        {
            this.Document.ExecCommand("Delete", false, null);
        }

        /// <summary>
        /// Find the first occurance of the given text.
        /// </summary>
        /// <param name="text">Text to find.</param>
        /// <returns>Found or not Found</returns>
        public bool FindFirst(string text)
        {
            IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
            IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
            sel.empty(); // get an empty selection, so we start from the beginning
            IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
            if (rng.findText(text, 1000000000, 0))
            {
                rng.select();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Finds occurances of the given text.
        /// </summary>
        /// <param name="text">Text to find.</param>
        /// <returns>Found or not Found</returns>
        public bool FindNext(string text)
        {
            IHTMLDocument2 doc = (IHTMLDocument2)this.Document.DomDocument;
            IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
            IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
            rng.collapse(false); // collapse the current selection so we start from the end of the previous range
            if (rng.findText(text, 1000000000, 0))
            {
                rng.select();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
